//
//  SYRegisterViewController.m
//  MicroNews
//
//  Created by siyue on 15/10/26.
//  Copyright © 2015年 siyue. All rights reserved.
//

#import "SYRegisterViewController.h"
#import "DialogUtil.h"
#import "SYLoginDataMaster.h"
#import "SVProgressHUD.h"

@interface SYRegisterViewController ()
- (IBAction)backClick:(id)sender;
- (IBAction)submitClick:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passWordTextField;
@property (weak, nonatomic) IBOutlet UITextField *passWord2TextField;

@end

@implementation SYRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - net

- (void)requestRegister
{
    [SVProgressHUD show];
    NSString *username = [NSString stringWithFormat:@"%@",self.userNameTextField.text];
    NSString *password = [NSString stringWithFormat:@"%@",self.passWordTextField.text];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",HomeURL,User_Register_URL];
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:username forKey:@"username"];
    [params setObject:password forKey:@"password"];
    [[NetworkInterface shareInstance] requestForPost:url parms:params complete:^(NSDictionary *result) {
        [SVProgressHUD dismiss];
        NSString *code = [NSString stringWithFormat:@"%@",result[@"code"]];
        if ([code isEqualToString:@"1"]) {
            NSString *message = [NSString stringWithFormat:@"%@",result[@"message"]];
            [[DialogUtil sharedInstance] showDlg:self.view textOnly:message];
            NSString *auth_code = [NSString stringWithFormat:@"%@",result[@"auth_code"]];
            NSString *member_id = [NSString stringWithFormat:@"%@",result[@"member_id"]];
            SetUserToken(auth_code);
            NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
            [userInfo setObject:member_id forKey:@"member_id"];
            [userInfo setObject:auth_code forKey:@"auth_code"];
            [[SYLoginDataMaster sharedInstance] saveLoginDataWithDic:userInfo];//保存登陆信息
            if (self.didRegister) {
                self.didRegister(username,password);
            }
        }
    } error:^(NSError *error) {
        [SVProgressHUD dismiss];
    }];
}

- (IBAction)backClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)submitClick:(id)sender {
    if ([self.userNameTextField.text isEqualToString:@""]) {
        [[DialogUtil sharedInstance] showDlg:self.view textOnly:@"请输入用户名"];
        return;
    }
    if ([self.passWordTextField.text isEqualToString:@""]) {
        [[DialogUtil sharedInstance] showDlg:self.view textOnly:@"请输入密码"];
        return;
    }
    if (![self.passWordTextField.text isEqualToString:self.passWord2TextField.text]) {
        [[DialogUtil sharedInstance] showDlg:self.view textOnly:@"两次输入密码必须一致"];
        return;
    }
    [self requestRegister];
}
@end
